package br.ufrj.neg.reuniao.servlet;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.velocity.context.Context;
import org.apache.velocity.tools.view.VelocityViewServlet;

import br.ufrj.neg.bean.Idioma;
import br.ufrj.neg.constants.General;
import br.ufrj.neg.reuniao.bean.ArtefatoBean;
import br.ufrj.neg.reuniao.bean.ReuniaoBean;
import br.ufrj.neg.reuniao.model.ArtefatoModel;
import br.ufrj.neg.reuniao.model.ReuniaoModel;
import br.ufrj.neg.servlet.PageContext;

public class CarregarRelatorioAction extends VelocityViewServlet {

	private static final long serialVersionUID = 3759364658257960936L;

	// private static Logger log = Logger.getLogger("rootLogger");

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		ReuniaoModel rm = new ReuniaoModel();
		ArtefatoModel am = new ArtefatoModel();
		Idioma idioma = (Idioma) request.getAttribute("idioma");
		ArtefatoBean ab = new ArtefatoBean(idioma);
			
		try {

			HttpSession session = request.getSession(false);

			synchronized (session) {
				session.removeAttribute("NegociacaoBean");

			}
			Integer idReuniao = Integer.parseInt(request
					.getParameter("idReuniao"));
			Integer idNegocio = Integer.parseInt(request
					.getParameter("idNegocio"));
			setContentType(request, response);

			Context context = createContext(request, response);
			fillContext(context, request);

			ab.setReuniao(idReuniao);
			ab = am.getRelatorioReuniao(ab);
			
						
			context.put("pageContext", new PageContext(request));
			context.put("reuniao", rm.getReuniao(idReuniao));			
			response.setContentType("application/pdf");
			response.setContentLength(ab.getArquivoAsByte().length);
			ServletOutputStream ouputStream = response.getOutputStream();
			ouputStream.write(ab.getArquivoAsByte(), 0, ab.getArquivoAsByte().length);
			ouputStream.flush();
			ouputStream.close();

		} catch (Exception e) {
			System.out.println(e);
			request.getRequestDispatcher(General.TELA_ERRO_500).forward(request,
					response);
		}
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		doPost(request, response);
	}

	public static InputStream byteToInputStream(byte[] bytes) throws Exception {
		ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
		return bais;
	}

	public static byte[] fileToByte(File imagem) throws Exception {
		FileInputStream fis = new FileInputStream(imagem);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		byte[] buffer = new byte[8192];
		int bytesRead = 0;
		while ((bytesRead = fis.read(buffer, 0, 8192)) != -1) {
			baos.write(buffer, 0, bytesRead);
		}
		return baos.toByteArray();
	}
}
